using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Altairis.Web.UI.WebControls {
    public class KeyedHyperLink : HyperLink {

        [Category("Behavior"), Bindable(true)]
        [Description("Numeric identifier of keyed hyperlink item.")]
        public int ItemId {
            get { return (int)this.ViewState["ItemId"]; }
            set { this.ViewState["ItemId"] = value; }
        }

        [Category("Behavior"), Bindable(true)]
        [Description("Strinkg title of keyed hyperlink item.")]
        public string Key {
            get { return (string)this.ViewState["Key"]; }
            set { this.ViewState["Key"] = value; }
        }

        protected override void Render(HtmlTextWriter writer) {
            string s = this.NavigateUrl;
            this.NavigateUrl = GetNavigateUrl();
            base.Render(writer);
            this.NavigateUrl = s;
        }

        private string GetNavigateUrl() {
            if (string.IsNullOrEmpty(this.Key)) {
                return string.Format(this.NavigateUrl, this.ItemId, KeyedUrl.CreateKey(this.Text));
            }
            else {
                return string.Format(this.NavigateUrl, this.ItemId, KeyedUrl.CreateKey(this.Key));
            }
        }

    }
}
